home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 November: Tool Chest / Dev.CD Nov 96 TC / Dev.CD Nov 96 TC.toast / Sample Code / Toolbox / RAMDisk 1.2 / Sources / RamCDev.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-07-19  |  11.8 KB  |  442 lines  |  [TEXT/MPS ]

  1. /*
  2. **    Apple Macintosh Developer Technical Support
  3. **
  4. **    RamCDev.c: Ram Disk Control Panel code
  5. **
  6. **    by Gordon Sheridan and Jim Luther
  7. **  modified by Brian Bechtel
  8. **
  9. **    File:        RamCDev.c
  10. **
  11. **    Copyright © 1992-1994 Apple Computer, Inc.
  12. **    All rights reserved.
  13. **
  14. **    You may incorporate this sample code into your applications without
  15. **    restriction, though the sample code has been provided "AS IS" and the
  16. **    responsibility for its operation is 100% yours.  However, what you are
  17. **    not permitted to do is to redistribute the source as "DTS Sample Code"
  18. **    after having made changes. If you're going to re-distribute the source,
  19. **    we require that you make it clear in the source that the code was
  20. **    descended from Apple Sample Code, but that you've made changes.
  21. **
  22. **    Change History (most recent first):
  23. **
  24. **    Change History (most recent first):
  25. **
  26. **         <3>     06/10/94    BL°B    Converted to work with Metrowerks & Symantec
  27. **         <2>     6/13/93    gs        Additional Cleanup
  28. **         <1>     6/13/93    gs        Clean up format.
  29. **        <0+>     10/2/92    gs        Clean up format.
  30. **/
  31.  
  32.  
  33. #include "RamCDev.h"
  34.  
  35. ////////////////////////////////// Prototypes ///////////////////////////////////
  36.  
  37. RDataHdl  DoInit(DialogPtr CPDialog, short numItems);
  38. RDataHdl  DoNul(RDataHdl cdevGlobalsHdl);
  39. RDataHdl  DoHit(RDataHdl cdevGlobalsHdl, short item);
  40. RDataHdl     DoClose(RDataHdl cdevGlobalsHdl);
  41. RDataHdl  DoUpdate(RDataHdl cdevGlobalsHdl);
  42. RDataHdl  DoActive(RDataHdl cdevGlobalsHdl);
  43. RDataHdl  DoDeactive(RDataHdl cdevGlobalsHdl);
  44. RDataHdl  DoKeyEvent(RDataHdl cdevGlobalsHdl, DialogPtr CPDialog, EventRecord *event);
  45. void     DrawItem (RDataHdl cdevGlobalsHdl, short item);
  46. Handle     IGetHand (RDataHdl cdevGlobalsHdl, short item);
  47. void     IGetRect (RDataHdl cdevGlobalsHdl, short item, Rect *itemRect);
  48. void     IInvalidate (RDataHdl cdevGlobalsHdl, short item);
  49. short     UpdateSysz(ConfigRecHandle config);
  50.  
  51.  
  52.  
  53. /////////////////////////////// Main Entry Point ////////////////////////////////
  54.  
  55. pascal RDataHdl
  56. main (short message, short item, short numItems, short CPanelID,
  57.          EventRecord *theEvent, RDataHdl cdevGlobalsHdl, DialogPtr CPDialog)
  58. {
  59. #pragma unused (CPanelID)        /* unused formal parameters */
  60.  
  61. #ifdef THINK_C
  62.     RememberA0();
  63.     SetUpA4();
  64. #endif
  65. #ifdef __MWERKS__
  66.     long oldA4=SetCurrentA4();
  67. #endif
  68.  
  69.     if (message == macDev) return((RDataHdl) 1);                /* we work on every machine */
  70.     else if (cdevGlobalsHdl != nil)
  71.     {
  72.         switch(message)
  73.         {
  74.             case nulDev:    cdevGlobalsHdl = DoNul(cdevGlobalsHdl);
  75.                             break;
  76.                             
  77.             case initDev:    cdevGlobalsHdl = DoInit(CPDialog,numItems);
  78.                             break;
  79.                             
  80.             case closeDev:    cdevGlobalsHdl = DoClose(cdevGlobalsHdl);
  81.                             break;
  82.  
  83.             case hitDev:    cdevGlobalsHdl = DoHit(cdevGlobalsHdl,item - numItems);
  84.                             break;
  85.                             
  86.             case updateDev:    cdevGlobalsHdl = DoUpdate(cdevGlobalsHdl);
  87.                             break;
  88.             
  89.             case activDev:    cdevGlobalsHdl = DoActive(cdevGlobalsHdl);
  90.                             break;
  91.             
  92.             case deactivDev:    
  93.                             cdevGlobalsHdl = DoDeactive(cdevGlobalsHdl);
  94.                             break;
  95.             
  96.             case keyEvtDev:
  97.                             cdevGlobalsHdl = DoKeyEvent(cdevGlobalsHdl,CPDialog,theEvent);
  98.                             break;
  99.                             
  100.             case undoDev:    /* not supported */        break;
  101.             case cutDev:    DlgCut(CPDialog);        break;
  102.             case copyDev:    DlgCopy(CPDialog);        break;
  103.             case pasteDev:    DlgPaste(CPDialog);        break;
  104.             case clearDev:    DlgDelete(CPDialog);    break;
  105.             
  106.             default:        // DebugStr("\pdefault:");
  107.                             break;
  108.         }
  109.     }
  110.  
  111. #ifdef THINK_C
  112.     RestoreA4();
  113. #endif
  114. #ifdef __MWERKS__
  115.     SetA4(oldA4);
  116. #endif
  117.     return cdevGlobalsHdl;
  118. }
  119.  
  120.  
  121.  
  122. ///////////////////////////// Function Definitions //////////////////////////////
  123.  
  124. RDataHdl  DoInit(DialogPtr CPDialog, short numItems)
  125. {
  126.     short    err;
  127.     Rect    r;
  128.     short    height;
  129.     long    myBufPtr;
  130.     
  131.     RDataHdl    cdevGlobalsHdl = (RDataHdl)NewHandle(sizeof(RDataStorage));
  132.     if (cdevGlobalsHdl == nil)    return cdevGlobalsHdl;
  133.  
  134.     HLock((Handle)cdevGlobalsHdl);
  135.     (**cdevGlobalsHdl).config    = (ConfigRecHandle)Get1Resource ('RDcf', 0);
  136.     err = ResError();
  137.     if ((**cdevGlobalsHdl).config == nil)
  138.     {
  139.         if (err == resNotFound || err == 0)    // then make a new one
  140.         {
  141.             (**cdevGlobalsHdl).config = (ConfigRecHandle)NewHandle(sizeof(ConfigRec));
  142.             // check config
  143.             (**(**cdevGlobalsHdl).config).install = false;
  144.             (**(**cdevGlobalsHdl).config).size     = 0;
  145.             GetIndString((**(**cdevGlobalsHdl).config).volumeName, rStringList, rDefaultNameStr);
  146.             if (ResError() != noErr)
  147.                 BlockMove("\pRamDisk",(**(**cdevGlobalsHdl).config).volumeName,8);
  148.             AddResource((Handle)(**cdevGlobalsHdl).config, 'RDcf', 0, "\p");
  149.             // check ResError();
  150.         }
  151.         else
  152.             DebugStr("\pGet1Resource('RDcf',0) returned nil...");
  153.     }
  154.     HLock((Handle)(**cdevGlobalsHdl).config);
  155.  
  156.     (**cdevGlobalsHdl).dlgPtr    = CPDialog;
  157.     (**cdevGlobalsHdl).dlgItems    = numItems;
  158.  
  159.     // initialize with config rsrc
  160.     SetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox),
  161.                 (**(**cdevGlobalsHdl).config).install);
  162.     (**cdevGlobalsHdl).cursize = (**(**cdevGlobalsHdl).config).size;
  163.     SetIText(IGetHand(cdevGlobalsHdl,iEditText),(**(**cdevGlobalsHdl).config).volumeName);
  164.     SelIText(CPDialog, numItems + iEditText, 0, 999); /* make caret show up */
  165.  
  166.     myBufPtr = (long)LMGetBufPtr();
  167.     (**cdevGlobalsHdl).maxsize    = ( myBufPtr / (1024L*1024L)) * 1024L;
  168.     if ((**cdevGlobalsHdl).maxsize < 0)    (**cdevGlobalsHdl).maxsize = 0;
  169.     NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  170.     (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  171.     
  172.     NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  173.     (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  174.  
  175.     IGetRect(cdevGlobalsHdl, iSizeBar, &r);
  176.     height = r.bottom - r.top - 4;
  177.     (**cdevGlobalsHdl).sizepos    = 
  178.         (**cdevGlobalsHdl).cursize/(double)(**cdevGlobalsHdl).maxsize * height;
  179.  
  180.     (**cdevGlobalsHdl).sizePict    = GetPicture (rCDevSizeBarPICT);
  181.     if ((**cdevGlobalsHdl).sizePict == nil)
  182.         DebugStr("\pGetPicture returned nil!");
  183.         
  184.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  185.     HUnlock((Handle)cdevGlobalsHdl);
  186.     
  187.     return cdevGlobalsHdl;
  188. }
  189.  
  190. RDataHdl  DoNul(RDataHdl cdevGlobalsHdl)
  191. {
  192.     HLock((Handle)cdevGlobalsHdl);
  193.     HLock((Handle)(**cdevGlobalsHdl).config);
  194.  
  195.     (**(**cdevGlobalsHdl).config).install = 
  196.             GetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,iCheckBox));
  197.     
  198.     (**(**cdevGlobalsHdl).config).size = (**cdevGlobalsHdl).cursize;
  199.  
  200.     GetIText( IGetHand(cdevGlobalsHdl,iEditText),
  201.             (**(**cdevGlobalsHdl).config).volumeName);
  202.  
  203.     HUnlock((Handle)(**cdevGlobalsHdl).config);
  204.     HUnlock((Handle)cdevGlobalsHdl);
  205.     
  206.     return cdevGlobalsHdl;
  207. }
  208.  
  209. RDataHdl  DoHit(RDataHdl cdevGlobalsHdl, short item)
  210. {
  211.     short    value,oldSize,newSize, barHeight;
  212.     Point    mouse;
  213.     Rect    r;
  214.     
  215.     switch(item)
  216.     {
  217.         case iCheckBox:
  218.                         value = GetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item));
  219.                         SetCtlValue((ControlHandle)IGetHand(cdevGlobalsHdl,item),!value);
  220.                         (**(**cdevGlobalsHdl).config).install = !value;
  221.                         break;
  222.         case iSizeBar:
  223.                         oldSize = (**cdevGlobalsHdl).sizepos;
  224.                         IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  225.                         barHeight = r.bottom - r.top - 4;
  226.                         while(StillDown())
  227.                         {
  228.                             GetMouse(&mouse);
  229.                             IGetRect(cdevGlobalsHdl,iSizeBar,&r);
  230.                             InsetRect(&r,0,2);
  231.                             if (PtInRect(mouse,&r))
  232.                             {
  233.                                 newSize = r.bottom - mouse.v;
  234.                                 if ((newSize < (**cdevGlobalsHdl).sizepos) ||
  235.                                     (newSize > (**cdevGlobalsHdl).sizepos))
  236.                                 {
  237.                                     (**cdevGlobalsHdl).sizepos = newSize;
  238.                                     (**cdevGlobalsHdl).cursize =
  239.                                         newSize/ (double)barHeight * 
  240.                                         (**cdevGlobalsHdl).maxsize;
  241.                                     (**cdevGlobalsHdl).cursize =
  242.                                         ((**cdevGlobalsHdl).cursize/32) * 32;
  243.                                     DrawItem(cdevGlobalsHdl,iSizeBar);
  244.                                     DrawItem(cdevGlobalsHdl,iCurSize);
  245.                                 }
  246.                             }
  247.                             else
  248.                             {
  249.                                 if ((**cdevGlobalsHdl).sizepos != oldSize)
  250.                                 {
  251.                                     (**cdevGlobalsHdl).sizepos = oldSize;
  252.                                     (**cdevGlobalsHdl).cursize =
  253.                                         oldSize / (double)barHeight * 
  254.                                         (**cdevGlobalsHdl).maxsize;
  255.                                     (**cdevGlobalsHdl).cursize =
  256.                                         ((**cdevGlobalsHdl).cursize/32) * 32;
  257.                                     DrawItem(cdevGlobalsHdl,iSizeBar);
  258.                                     DrawItem(cdevGlobalsHdl,iCurSize);
  259.                                 }
  260.                             }
  261.                         }
  262.                         break;
  263.     }
  264.     return cdevGlobalsHdl;
  265. }
  266.  
  267. RDataHdl DoClose(RDataHdl cdevGlobalsHdl)
  268. {
  269.     short    err;
  270.     
  271.     if (cdevGlobalsHdl)
  272.     {
  273.         if ( (**cdevGlobalsHdl).config)
  274.         {
  275.             err = UpdateSysz((**cdevGlobalsHdl).config);
  276.         
  277.             HLock((Handle)cdevGlobalsHdl);
  278.             ChangedResource((Handle)(**cdevGlobalsHdl).config);
  279.             WriteResource((Handle)(**cdevGlobalsHdl).config);
  280.             ReleaseResource((Handle)(**cdevGlobalsHdl).config);
  281.         // check for ResError()!!!
  282.             HUnlock((Handle)cdevGlobalsHdl);
  283.         }
  284.     
  285.         DisposHandle((Handle)cdevGlobalsHdl);
  286.         cdevGlobalsHdl = nil;
  287.     }
  288.     return cdevGlobalsHdl;
  289. }
  290.  
  291. RDataHdl  DoUpdate(RDataHdl cdevGlobalsHdl)
  292. {
  293.     DrawItem(cdevGlobalsHdl, iSizeBar);
  294.     DrawItem(cdevGlobalsHdl, iMaxSize);
  295.     DrawItem(cdevGlobalsHdl, iCurSize);
  296.     
  297.     return cdevGlobalsHdl;
  298. }
  299.  
  300. RDataHdl  DoActive(RDataHdl cdevGlobalsHdl)
  301. {
  302.     return cdevGlobalsHdl;
  303. }
  304.  
  305. RDataHdl  DoDeactive(RDataHdl cdevGlobalsHdl)
  306. {
  307.     return cdevGlobalsHdl;
  308. }
  309.  
  310. RDataHdl  DoKeyEvent(RDataHdl cdevGlobalsHdl,
  311.                  DialogPtr CPDialog,
  312.                  EventRecord *event)
  313. {
  314.     char    tempChar;
  315.     
  316.     tempChar = event->message & charCodeMask;    // get the character, and check
  317.     if (event->modifiers & cmdKey)                //  status of command key
  318.     {            
  319.         event->what = nullEvent;                // and empty event type
  320.         
  321.         switch (tempChar)                        // set appropriate message
  322.         {
  323.             case 'X':
  324.             case 'x':    DlgCut(CPDialog);    break;
  325.             
  326.             case 'C':
  327.             case 'c':    DlgCopy(CPDialog);    break;
  328.             
  329.             case 'V':
  330.             case 'v':    DlgPaste(CPDialog);    break;
  331.         }
  332.     }
  333.     return cdevGlobalsHdl;
  334. }
  335.  
  336. void  DrawItem (RDataHdl cdevGlobalsHdl, short item)
  337. {
  338.     Rect        itemRect,r;
  339.     long        len;
  340.     RgnHandle    rgn = NewRgn();
  341.     
  342.     IGetRect(cdevGlobalsHdl, item, &itemRect);
  343.     SetPort( (**cdevGlobalsHdl).dlgPtr);
  344.     GetClip(rgn);
  345.  
  346.     TextMode(srcCopy);
  347.  
  348.     switch(item)
  349.     {
  350.         case iSizeBar:
  351.                         r = itemRect;
  352.                         FrameRect(&r);
  353.                         r.bottom = r.bottom - (**cdevGlobalsHdl).sizepos;
  354.                         ClipRect(&r);
  355.                         DrawPicture((**cdevGlobalsHdl).sizePict,&itemRect);
  356.                         SetClip(rgn);
  357.                         InsetRect(&itemRect,2,2);
  358.                         itemRect.top = itemRect.bottom - (**cdevGlobalsHdl).sizepos;
  359.                         PaintRect(&itemRect);
  360.                         break;
  361.         case iMaxSize:
  362.                         NumToString((**cdevGlobalsHdl).maxsize,(**cdevGlobalsHdl).maxStr);
  363.                         (**cdevGlobalsHdl).maxStr[++(**cdevGlobalsHdl).maxStr[0]] = 'K';
  364.                         len = (**cdevGlobalsHdl).maxStr[0];
  365.                         TextBox (&(**cdevGlobalsHdl).maxStr[1], len, &itemRect, teJustRight);                        
  366.                         break;
  367.         case iCurSize:    
  368.                         NumToString((**cdevGlobalsHdl).cursize,(**cdevGlobalsHdl).curStr);
  369.                         (**cdevGlobalsHdl).curStr[++(**cdevGlobalsHdl).curStr[0]] = 'K';
  370.                         len = (**cdevGlobalsHdl).curStr[0];
  371.                         TextBox (&(**cdevGlobalsHdl).curStr[1], len, &itemRect, teJustRight);                        
  372.                         break;
  373.     }
  374.      
  375.     TextMode(srcOr);
  376. }
  377.  
  378. Handle    IGetHand (RDataHdl cdevGlobalsHdl, short item)
  379. {
  380.     Handle    itemHand;
  381.     Rect    itemRect;
  382.     short    itemType;
  383.     
  384.     GetDItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  385.                 &itemType, &itemHand, &itemRect);
  386.     return itemHand;
  387. }
  388.  
  389. void    IGetRect (RDataHdl cdevGlobalsHdl, short item, Rect *itemRect)
  390. {
  391.     short    itemType;
  392.     Handle    itemHand;
  393.     
  394.     GetDItem( (**cdevGlobalsHdl).dlgPtr, item + (**cdevGlobalsHdl).dlgItems,
  395.                 &itemType, &itemHand, itemRect);
  396. }
  397.  
  398. void    IInvalidate (RDataHdl cdevGlobalsHdl, short item)
  399. {
  400.     Rect    itemRect;
  401.     
  402.     IGetRect(cdevGlobalsHdl, item, &itemRect);
  403.     EraseRect(&itemRect);
  404.     InvalRect(&itemRect);
  405. }
  406.  
  407. short    UpdateSysz(ConfigRecHandle config)
  408. {
  409.     Handle    sysz;
  410.     short    err = 0;
  411.     
  412.     sysz = Get1Resource ('sysz', 0);
  413.     if (sysz == nil)
  414.     {
  415.         err = ResError();
  416.         if (err == resNotFound || err == 0)    // then make a new one
  417.         {
  418.             sysz = NewHandle(sizeof(long));
  419.             if (sysz == nil)
  420.                 return -1;
  421.             
  422.             **(long **)sysz = 0;
  423.                 
  424.             AddResource(sysz, 'sysz', 0, "\p");
  425.             err =  ResError();
  426.             if (err)
  427.                 return err;
  428.         }
  429.         else
  430.             DebugStr("\pGet1Resource('sysz',0) returned nil...");
  431.     }
  432.     
  433.     if ( (**config).install)
  434.         **(long **)sysz = (16 + (**config).size) * 1024;
  435.     else
  436.         **(long **)sysz = 16 * 1024;
  437.     ChangedResource(sysz);
  438.     WriteResource(sysz);
  439.     ReleaseResource(sysz);
  440.     
  441.     return err;
  442. }